package com.stone.databinding.data;

import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

public class ProfileLiveDataViewModel extends ViewModel {
    private MutableLiveData<String> _name = new MutableLiveData<>();
    private MutableLiveData<String> _lastName = new MutableLiveData<>();
    private MutableLiveData<Integer> likesNum = new MutableLiveData<>();

    public ProfileLiveDataViewModel(String name, String lastName, int likes) {
        _name.setValue(name);
        _lastName.setValue(lastName);
        likesNum.setValue(likes);
    }

    public ProfileLiveDataViewModel() {
        _name.setValue("Dan");
        _lastName.setValue("Vie");
        likesNum.setValue(0);
    }

    private LiveData<String> firstName = _name;
    private LiveData<String> lastName = _lastName;
    private LiveData<Integer> likes = likesNum;

    public LiveData<String> getFirstName() {
        return firstName;
    }

    public void setFirstName(LiveData<String> firstName) {
        this.firstName = firstName;
    }

    public LiveData<String> getLastName() {
        return lastName;
    }

    public void setLastName(LiveData<String> lastName) {
        this.lastName = lastName;
    }

    public LiveData<Integer> getLikes() {
        return likes;
    }

    public void setLikes(LiveData<Integer> likes) {
        this.likes = likes;
    }

    public LiveData<Popularity> getPopularity() {
        return popularity;
    }

    public void setPopularity(LiveData<Popularity> popularity) {
        this.popularity = popularity;
    }

    // popularity is exposed as LiveData using a Transformation instead of a @Bindable property.
    LiveData<Popularity> popularity = Transformations.map(likes, new Function<Integer, Popularity>() {
        @Override
        public Popularity apply(Integer input) {
        if (input > 9) {
            return Popularity.STAR;
        } else if (input > 4) {
            return Popularity.POPULAR;
        } else {
            return Popularity.NORMAL;
        }
        }
    });

    public void onLike() {
        likesNum.setValue(likesNum.getValue() > -1 ? likesNum.getValue() : 0);
    }
}